//
//  HomePageListPicView.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//

#define itemPadding 1.0
#define itemWidth 86
#import "HomePageUserInfoPicView.h"

@interface HomePageUserInfoPicView ()
@property(nonatomic, strong) UIView *singalView; // 只有一张图
@property(nonatomic, strong) UIView *doubleView; // 2张 or 4张图
@property(nonatomic, strong) UIView *normalTypeView; // 其他
@property(nonatomic, strong) UIView *otherTypeView; // 三张
@end

@implementation HomePageUserInfoPicView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

                    
- (void)setUI{
    

    [self addSubview:self.singalView];
    [self.singalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(itemWidth);
        make.height.mas_equalTo(itemWidth);
    }];
    
    [self addSubview:self.doubleView];
    [self.doubleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(itemWidth);
        make.height.mas_equalTo(itemWidth);
    }];
    
    [self addSubview:self.normalTypeView];
    [self.normalTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(itemWidth);
        make.height.mas_equalTo(itemWidth);
    }];
    
    [self addSubview:self.otherTypeView];
    [self.otherTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(itemWidth);
        make.height.mas_equalTo(itemWidth);
    }];
}

- (void)setVideoUrl:(NSString *)url{
    self.singalView.hidden = NO;
    self.doubleView.hidden = YES;
    self.normalTypeView.hidden = YES;
    self.otherTypeView.hidden = YES;
    [self.singalView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
    [self.singalView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj sd_setImageWithURL:[NSURL URLWithString:url]];
    }];
    
    [self.singalView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(114);
    }];
}

- (void)setIndex:(NSNumber *)index urls:(NSArray *)urls{
    if (index.intValue == 1) {
        self.singalView.hidden = NO;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = YES;
        [self.singalView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.singalView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]]];
        }];
        [self.singalView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(itemWidth);
        }];
    }
    
    if (index.intValue == 2) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = NO;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = YES;
        [self.doubleView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.doubleView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]]];
        }];
    }
    if (index.intValue == 3) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = NO;
        [self.otherTypeView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.otherTypeView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]]];
        }];
    }
    if (index.intValue == 4) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = NO;
        self.otherTypeView.hidden = YES;
        [self.normalTypeView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.normalTypeView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]]];
        }];
    }
}

- (UIView *)singalView{
    
    if (!_singalView) {
        _singalView = [[UIView alloc] init];
        _singalView.hidden = YES;
        UIImageView *img = [[UIImageView alloc] init];
        img.backgroundColor = RGBA(240, 240, 240, 1);
        img.userInteractionEnabled = YES;
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.layer.masksToBounds = YES;
        [_singalView addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.singalView);
            make.top.equalTo(self.singalView);
            make.right.bottom.equalTo(self.singalView);
        }];
    }
    return _singalView;
}

- (UIView *)doubleView{
    
    if (!_doubleView) {
        _doubleView = [[UIView alloc] init];
        _doubleView.hidden = YES;
    
        CGFloat picW = (itemWidth - itemPadding)/2;
    
        for (NSInteger i = 0; i < 2; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.userInteractionEnabled = YES;
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.masksToBounds = YES;
            [_doubleView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(picW, itemWidth));
                make.top.equalTo(self.doubleView);
                make.left.mas_equalTo(self.doubleView).offset(i*(picW + itemPadding));
            
            }];
        }
    }
    return _doubleView;
}

- (UIView *)normalTypeView{
    
    if (!_normalTypeView) {
        _normalTypeView = [[UIView alloc] init];
        _normalTypeView.hidden = YES;
        CGFloat picW = (itemWidth - itemPadding)/2;
        UIImageView *lastImg = nil;
        for (NSInteger i = 0; i < 4; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.userInteractionEnabled = YES;
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.masksToBounds = YES;
            [_normalTypeView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                if (lastImg) {
                    if (i % 2 == 0) {
                        make.left.equalTo(self.normalTypeView);
                        make.top.equalTo(lastImg.mas_bottom).offset(itemPadding);
                    } else {
                        make.left.equalTo(lastImg.mas_right).offset(itemPadding);
                        make.top.equalTo(lastImg);
                    }
                } else {
                    make.left.equalTo(self.normalTypeView);
                    make.top.equalTo(self.normalTypeView);
                }
                make.size.mas_equalTo(CGSizeMake(picW, picW));
            }];
            lastImg = img;
        }
    }
    return _normalTypeView;
}

- (UIView *)otherTypeView{
    if (!_otherTypeView) {
        _otherTypeView = [[UIView alloc] init];
        _otherTypeView.hidden = YES;
        CGFloat picW = (itemWidth - itemPadding)/2;
        CGFloat max = 3;
        UIImageView *lastImg = nil;
        for (int i = 0; i < max; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.userInteractionEnabled = YES;
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.masksToBounds = YES;
            [_otherTypeView addSubview:img];
            
            if (i == max - 1) {
                [img mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastImg.mas_right).offset(itemPadding);
                    make.top.equalTo(self.otherTypeView);
                    make.size.mas_equalTo(CGSizeMake(picW, itemWidth));
                }];
            }else{
                [img mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.otherTypeView);
                    make.top.equalTo(self.otherTypeView).offset(i * (picW + itemPadding));
                    make.size.mas_equalTo(CGSizeMake(picW, picW));
                }];
            }
            lastImg = img;
        }
        
    }
    return _otherTypeView;
}

@end
